package com.zte.warehouse.control;


import com.zte.sys.login.model.LUsers;
import com.zte.util.OperatorLog;
import com.zte.warehouse.model.*;
import com.zte.warehouse.service.IWarehouseService;
import com.zte.util.PageModel;
import com.zte.util.ResultMessage;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 仓库管理模块
 * <p>
 * 仓库管理模块使用者：财务部门经理  job_id=6
 * 创建仓库权限：财务部门经理
 */


@Controller
@RequestMapping("/Warehouse")
public class WarehouseAction {
    @Autowired
    private IWarehouseService warehouseService;
    @Autowired
    private RedisTemplate redisTemplate;


    @RequestMapping("/WarehousePage")
    @ResponseBody
    @OperatorLog(operate="多条件仓库列表查询分页", module="仓库管理")
    public ResultMessage getWarehousesPage(PageModel<Warehouse> pageModel, WarehouseCondition conn) {

        //1.身份验证：判断用户jobID, 财务部门经理看全部,非财务部门经理看自己
        LUsers User = (LUsers) redisTemplate.opsForValue().get(conn.getUserID() + "");
        ResultMessage msg = new ResultMessage();
        if (User != null) {
            if (User.getJobId() == 6l||User.getJobId() == 1l) {//财务部门经理、总经理
                conn.setUserID(null);
//                System.out.println("JobId:"+User.getJobId());
            }
//            System.out.println("NowUser:" + User);
        }
        System.out.println(conn);
        PageModel<Warehouse> warehousePageModel = warehouseService.queryWarehousePageByCondition(pageModel, conn);
        msg.setStatus(200);
        msg.setMsg("查询成功！");
        msg.setData(warehousePageModel);

        return msg;
    }

    @RequestMapping("/WarehouseList")
    @ResponseBody
    @OperatorLog(operate="加载仓库列表", module="仓库管理")
    public ResultMessage getWarehousesList() {

        ResultMessage msg = new ResultMessage();
        List<SingleWarehouse> singleWarehouses = warehouseService.queryWarehouseList();
        msg.setStatus(200);
        msg.setMsg("查询成功！");
        msg.setData(singleWarehouses);

        return msg;
    }

    @RequestMapping("/WarehouseDetail")
    @ResponseBody
    @OperatorLog(operate="条件查询仓库详情", module="仓库管理")
    public ResultMessage getWarehouse(WarehouseCondition conn, HttpSession httpSession) {

        System.out.println(conn);
//        Object storageViewid = redisTemplate.opsForValue().get("storageViewid");
        Object storageViewid =  httpSession.getAttribute("storageViewid");
        System.out.println("查到了id" + storageViewid);

        if(storageViewid!=null){conn.setWarehouseID((int) storageViewid);}
        Integer WarehouseID=  conn.getWarehouseID();
        ResultMessage msg = new ResultMessage();
        if ( WarehouseID!= null) {
            Warehouse warehouse = warehouseService.queryUserByCondition(conn);
            msg.setStatus(200);
            msg.setMsg("查询成功！");
            msg.setData(warehouse);
        } else {
            msg.setStatus(401);
            msg.setMsg("仓库ID为空！");
        }

        return msg;
    }

    @SneakyThrows
    @RequestMapping("/WarehouseAdd")
    @ResponseBody
    @OperatorLog(operate="创建仓库", module="仓库管理")
    public ResultMessage addWarehouse(GetWarehouse getWarehouse) {
        System.out.println("getWarehouse"+getWarehouse);

        //处理传入数据
        //wmoney
        Double money = 0d;
        String wmoney = getWarehouse.getWMoney();
        if (getWarehouse.getWMoney() != null) {
            money = Double.parseDouble(getWarehouse.getWMoney());
        }
        //时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date creatTime = sdf.parse(getWarehouse.getCreationTime());

        SingleWarehouse singleWarehouse = new SingleWarehouse();
//        null, getWarehouse.getCName(), getWarehouse.getPhone(),
//                getWarehouse.getCAddress(), getWarehouse.getState(), creatTime, money,
//                getWarehouse.getDetails(), Integer.parseInt(getWarehouse.getUsre()),Integer.parseInt(getWarehouse.getUsers()),
//                Integer.parseInt(getWarehouse.getProvince()), Integer.parseInt(getWarehouse.getCity()));

        singleWarehouse.setCName(getWarehouse.getCName());
        singleWarehouse.setPhone(getWarehouse.getPhone());
        singleWarehouse.setCAddress(getWarehouse.getCAddress());
        singleWarehouse.setState(getWarehouse.getState());
        singleWarehouse.setCreationTime(creatTime);
        singleWarehouse.setWMoney(money);

        if(getWarehouse.getDetails()!=null)
        singleWarehouse.setDetails(getWarehouse.getDetails());
        else
            singleWarehouse.setDetails("");
        singleWarehouse.setUsers(Integer.parseInt(getWarehouse.getUsre()));
        singleWarehouse.setUsre(Integer.parseInt(getWarehouse.getUsre()));
        singleWarehouse.setProvince(Integer.parseInt(getWarehouse.getProvince()));
        singleWarehouse.setCity(Integer.parseInt(getWarehouse.getCity()));



        System.out.println(getWarehouse);
        System.out.println(singleWarehouse);

        int i = warehouseService.addWarehouse(singleWarehouse);
        ResultMessage msg = new ResultMessage();
        msg.setStatus(200);
        msg.setMsg("添加成功！");
        msg.setData(i);
        return msg;
    }

    //加载财务部仓管
    @RequestMapping("/WarehouseGetUsre")
    @ResponseBody
    public ResultMessage GetUsre() {
        List<getUserMSG> warehouseUsre = warehouseService.getWarehouseUsre();
        ResultMessage msg = new ResultMessage();
        msg.setStatus(200);
        msg.setMsg("加载财务部仓管！");
        msg.setData(warehouseUsre);
        return msg;
    }

    @RequestMapping("/WarehouseUpdate")
    @ResponseBody
    @OperatorLog(operate="仓库信息更新", module="仓库管理")
    public ResultMessage updateWarehouse(GetWarehouse getWarehouse,HttpSession httpSession) {
        System.out.println(getWarehouse);

        Integer storageUpdateid = (Integer)httpSession.getAttribute("storageUpdateid");

//        Integer storageUpdateid = (Integer) redisTemplate.opsForValue().get("storageUpdateid");//仓库id
        System.out.println(storageUpdateid);
        SingleWarehouse singleWarehouse = new SingleWarehouse();
        singleWarehouse.setId(storageUpdateid);
        singleWarehouse.setCName(getWarehouse.getCName());
        singleWarehouse.setPhone(getWarehouse.getPhone());
        singleWarehouse.setCAddress(getWarehouse.getCAddress());
        singleWarehouse.setState(getWarehouse.getState());
        singleWarehouse.setDetails(getWarehouse.getDetails());
        singleWarehouse.setUsre(Integer.parseInt(getWarehouse.getUsre()));
        singleWarehouse.setProvince(Integer.parseInt(getWarehouse.getProvince()));
        singleWarehouse.setCity(Integer.parseInt(getWarehouse.getCity()));

        System.out.println(getWarehouse);
        System.out.println(singleWarehouse);

        int i = warehouseService.updateWarehouse(singleWarehouse);
        ResultMessage msg = new ResultMessage();
        msg.setStatus(200);
        msg.setMsg("更新成功！");
        msg.setData(i);
        return msg;
    }

    @RequestMapping("/updateWarehouseState")
    @ResponseBody
    @OperatorLog(operate="更新仓库状态", module="仓库管理")
    public ResultMessage updateWarehouseState(SingleWarehouse singleWarehouse) {
        System.out.println(singleWarehouse);

        int i = warehouseService.updateWarehouse(singleWarehouse);
        ResultMessage msg = new ResultMessage();
        msg.setStatus(200);
        msg.setMsg("更新状态成功！");
        msg.setData(i);
        return msg;
    }

}
